package com.invest.infra.template;

import com.invest.utils.io.FileIOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * STMS 2012
 * 
 * @author Evan
 * @date 2012-09-15
 * 
 */
public class ExportResponseUtils {

	private static final Logger LOGGER = LoggerFactory.getLogger(ExportResponseUtils.class);
	private static int BUFFER_SIZE = 4096;

	/**
	 * reset the response for zip
	 * @param response
	 * @param fileName
	 */
	public static void setResponse4Zip(HttpServletResponse response, String fileName) {
		
		response.reset();
		response.setContentType("application/zip;charset = utf-8");
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".zip");
		response.setBufferSize(BUFFER_SIZE);
	}
	
	/**
	 * reset the reponse for excel
	 * @param response
	 * @param fileName
	 */
	public static void setResponse4Excel(HttpServletResponse response, String fileName) {

		response.reset();
		response.setContentType("application/xls;charset = utf-8");
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
		response.setBufferSize(BUFFER_SIZE);
		
	}
	
	/**
	 * reset the response for pdf
	 * @param pdfName
	 * @param response
	 */
	public static void setResponse4Pdf( String pdfName , HttpServletResponse response){
		
		response.reset();
		response.setContentType("application/pdf;charset =utf-8");
		response.setHeader("Content-Disposition", "attachment; filename="+pdfName+".pdf");
		response.setBufferSize(BUFFER_SIZE);
		
	}
	
	/**
	 * encoding the file name by browse type
	 * @param fileName
	 * @param request
	 * @return
	 */
	public static String encodeFileName(String fileName,HttpServletRequest request){
		//for other browsers 
		String name = fileName;
		try {
			name = URLEncoder.encode(fileName, "utf-8");
			//for firefox 
			if(request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){
				name = new String(fileName.getBytes("utf-8"),"ISO8859-1");
			}
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		
		return name;
	}
	
	public static void downFile(String fileName,String path,HttpServletResponse res){
			
			OutputStream out = null;
			BufferedInputStream in = null;
		    try {  
		    	out = res.getOutputStream();  
		        res.reset();  
		        res.setHeader("Content-Disposition", "attachment; filename="+fileName);  
		        res.setContentType("application/octet-stream; charset=utf-8");  
		        
		        in = FileIOUtils.fileBufferedInputStream(path);
		        byte[] arr = new byte[BUFFER_SIZE];
		        for(int len = in.read(arr);len != -1;len=in.read(arr)){
		        	out.write(arr);  
		        }
		        
		    } catch (IOException e) {
				LOGGER.error("downFile exception",e);
			}finally{
				
				FileIOUtils.close(out);
				FileIOUtils.close(in);
			}
		
	}
}
